package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Transactional(rollbackFor = Exception.class)
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService   {


    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {

        //判断前段传递过来的参数是否为null
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页
        dto.checkParam();

        //封装条件  执行查询
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();
        //频道平层  判断传递过来的 name 是否有
        if (StringUtils.isNotBlank(dto.getName())) {
            //模糊查询
            wrapper.like(AdChannel::getName , dto.getName());
        }
        //判断状态
        if (dto.getStatus() != null){
            //根据状态查询
            wrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //排序   按照排序规则
        wrapper.orderByAsc(AdChannel::getOrd);

        //分页  页数  条数
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());

        IPage<AdChannel> page1 = this.page(page, wrapper);

        //封装数据返回
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());


        return responseResult;
    }


    /**
     * 新增
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //校验参数
        //名字不为null  不能大于10个字符
        String name = channel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不为控");
        }
        if (name.length() >10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能大于10");
        }
        //名称不重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已存在");
        }
        //保存频道
        channel.setCreatedTime(new Date());
        save(channel);

        //返回结果

        return ResponseResult.okResult();
    }


    /**
     * 修改
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //校验参数
            //校验id
            if (adChannel.getId() == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id 不能为空");
            }
            //查询平道
        AdChannel channel = this.getById(adChannel.getId());
            if (channel==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道不存在");
            }
        //参看名字是否为null
        String name = channel.getName();
        //和上次是否一致
        if (StringUtils.isNotBlank(name) && !name.equals(adChannel.getName())){
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已存在");
            }
        }
        //todo
        //修改数据
        updateById(adChannel);

        //返回结果
        return ResponseResult.okResult();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //检查id
        if (id == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }

        //查询频道
        AdChannel channel = this.getById(id);
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道不存在");
        }

        //查询状态
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"需要先下架才能删除");
        }

        //删除用户
        removeById(id);
        //返回成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
